package org.lzfto.flink.demo.controller;

import java.util.List;
import java.util.Map;

import org.lzfto.flink.demo.business.student.Student;
import org.lzfto.flink.demo.business.student.StudentService;
import org.lzfto.flink.demo.common.result.PubResult;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Lazy
@RestController
@RequestMapping("/students")
public class StudentController {

    private final StudentService studentService;
    
    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }
    
    @PostMapping("/add")
    public ResponseEntity<Void> createStudent(@RequestBody Student student) {
        studentService.create(student);
        return ResponseEntity.ok().build();
    }

    @GetMapping
    public ResponseEntity<List<Student>> getAllStudents(Student condition) {
        return ResponseEntity.ok(studentService.findAll(condition));
    }

    @GetMapping("/getById/{id}")
    public ResponseEntity<Student> getStudentById(@PathVariable int id) {
        return ResponseEntity.ok(studentService.findById(id));
    }

    @PostMapping("/update/{id}")
    public ResponseEntity<Void> updateStudent(@PathVariable int id, @RequestBody Student student) {
        student.setStudentId(id);
        studentService.update(student);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/delete/{id}")
    public ResponseEntity<Void> deleteStudent(@PathVariable int id) {
        studentService.delete(id);
        return ResponseEntity.ok().build();
    }

	@GetMapping("/sumByGender")
    public PubResult<List<Map<String, Object>>> sumByGender() {
        return studentService.sumByGender();
    }
}
